package com.hutu.erp.catalog.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.hutu.erp.catalog.domain.ProductPicture;
import com.hutu.erp.catalog.dto.ProductPictureResult;
import com.hutu.erp.catalog.map.CatalogMap;
import com.hutu.erp.catalog.mapper.ProductPictureMapper;
import com.hutu.erp.catalog.model.ProductPictureModel;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.Picture;
import com.ruoyi.system.service.IPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hutu.erp.catalog.mapper.ProductMapper;
import com.hutu.erp.catalog.domain.Product;
import com.hutu.erp.catalog.service.IProductService;

import javax.annotation.Resource;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-24
 */
@Service
public class ProductServiceImpl implements IProductService 
{
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductPictureMapper productPictureMapper;

    @Autowired
    private IPictureService pictureService;

    @Resource
    private CatalogMap catalogMap;

    @Autowired
    private ServerConfig serverConfig;



    /**
     * 查询商品图片列表
     *
     * @param productId 商品Id
     * @return 商品图片集合
     */
    @Override
    public List<ProductPicture> getProductPictureList(long productId) {
        if(productId < 0){
            return new ArrayList<>();
        }
        return productPictureMapper.getProductPictureList(productId);
    }

    /**
     * 新增商品图片
     *
     * @param productPicture 商品图片
     * @return 结果
     */
    @Override
    public int insertProductPicture(ProductPicture productPicture){
        return productPictureMapper.insertProductPicture(productPicture);
    }

    /**
     * 新增商品图片
     *
     * @param model 商品图片
     * @return 结果
     */
    @Override
    public ProductPictureResult insertProductPicture(ProductPictureModel model){

        Picture picture = catalogMap.toPicture(model);
        picture.setCreatedBy(SecurityUtils.getUsername());
        int i = pictureService.insertPicture(picture);
        System.out.println("插入图片的自增Id： " + picture.getPictureId());

        ProductPicture productPicture = catalogMap.toProductPicture(model);
        productPicture.setPictureId(picture.getPictureId());
        Integer displayOrder = productPicture.getDisplayOrder();
        if(displayOrder == null || displayOrder.equals(0)){
            productPicture.setDisplayOrder(1);
        }

        productPictureMapper.insertProductPicture(productPicture);
        ProductPictureResult result = new ProductPictureResult();

        result.setProductPictureId(productPicture.getProductPictureId());
        result.setProductId(productPicture.getProductId());
        result.setDisplayOrder(productPicture.getDisplayOrder());
        result.setPictureId(picture.getPictureId());
        result.setUrl(serverConfig.getUrl() +  picture.getVirtualPath());

        return result;
    }

    @Override
    public ProductPictureResult updateProductPictures(ArrayList<ProductPictureModel>  model)
    {
        ProductPictureResult result = new ProductPictureResult();
        if(model == null || model.size() == 0){
            return result;
        }
        for(ProductPictureModel ppm : model){

            ProductPicture productPicture = catalogMap.toProductPicture(ppm);
            System.out.println("productPicture:" + ppm.toString() );
//            productPicture.setProductPictureId(ppm.getProductPictureId());
            System.out.println("更新productPicture:" + productPicture.toString() );
            productPictureMapper.updateProductPicture(productPicture);
        }
        return result;
    }

    /**
     * 查询商品
     * 
     * @param productId 商品ID
     * @return 商品
     */
    @Override
    public Product selectProductById(Long productId)
    {
        return productMapper.selectProductById(productId);
    }

    /**
     * 查询商品列表
     * 
     * @param product 商品
     * @return 商品
     */
    @Override
    public List<Product> selectProductList(Product product)
    {
        return productMapper.selectProductList(product);
    }

    /**
     * 新增商品
     * 
     * @param product 商品
     * @return 结果
     */
    @Override
    public int insertProduct(Product product)
    {
        System.out.println("输出一下 product，查看一下页面上采集到的数据。");
        System.out.println(product);
        product.setCreatedBy(SecurityUtils.getUsername());
        product.setUpdatedBy(SecurityUtils.getUsername());
        return productMapper.insertProduct(product);
    }

    /**
     * 修改商品
     * 
     * @param product 商品
     * @return 结果
     */
    @Override
    public int updateProduct(Product product)
    {
        System.out.println("输出一下 product，查看一下页面上采集到的数据。");
        System.out.println(product);
        product.setUpdatedBy(SecurityUtils.getUsername());

        return productMapper.updateProduct(product);
    }

    /**
     * 批量删除商品
     * 
     * @param productIds 需要删除的商品ID
     * @return 结果
     */
    @Override
    public int deleteProductByIds(Long[] productIds)
    {
        return productMapper.deleteProductByIds(productIds);
    }

    /**
     * 删除商品信息
     * 
     * @param productId 商品ID
     * @return 结果
     */
    @Override
    public int deleteProductById(Long productId)
    {
        return productMapper.deleteProductById(productId);
    }
}
